/**
 * 
 */
package com.android.hw.projectmenu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import com.android.hw.projectmenu.R;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.text.TextUtils;
import android.util.Log;
import android.os.SystemProperties;
/**
 * @author gaoshengpan
 *
 */
public class HardwareInfo extends PreferenceActivity {
    
        private static final String KEY_BOARD_ID = "board_id";
        private static final String KEY_TOUCHSCREEN_TYPE = "touchscreen_type";
        private static final String KEY_CAMERA_TYPE = "main_camera_type";
        private static final String KEY_CAMERA_TYPE1 = "auxi_camera_type";
        private static final String KEY_FLASH_TYPE = "flash_type";
        private static final String KEY_DDR_TYPE = "ddr_type";
        private static final String KEY_LCD_TYPE = "lcd_type";
        private static final String KEY_BATTERY_TYPE = "battery_type";
        private static final String KEY_GESENSOR_TYPE = "gsensor_type";
        private static final String KEY_LIGHT_TYPE = "light_type";
        private static final String KEY_WFBTFMGPS_TYPE = "wfbtfmgps_info";
        private static String mUnknown = null;
        String boardInfo = "msm8916-titan";
        String hwInfo = "";
        private String TAG = "HardwareInfo";

    /**
     * 
     */
    public HardwareInfo() {
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.hardware_info);
        Log.d(TAG, "ddr---------------------->"+readFileFile("/sys/bus/mmc/devices/mmc0:0001/name"));
        Log.d(TAG, "lcd---------------------->"+readFileFile("/sys/class/graphics/fb0/msm_fb_panel_info"));
        Log.d(TAG, "tosc-----------ic----------->"+readFileFile("/sys/class/assist/ctp/ic"));
        Log.d(TAG, "tosc-----------gujian----------->"+readFileFile("/sys/class/assist/ctp/fw_ver"));
        Log.d(TAG, "tp-----------factoty----------->"+readFileFile("/sys/class/assist/ctp/tp_vendor"));
        Log.d(TAG, "tp-----------success----------->"+readFileFile("/sys/class/assist/ctp/exist"));
        Log.d(TAG, "gsensor-----------ic----------->"+readFileFile("/sys/class/assist/accel/ic"));
        Log.d(TAG, "gsensor-----------factoty----------->"+readFileFile("/sys/class/assist/accel/vendor"));
        Log.d(TAG, "gsensor-----------probe----------->"+readFileFile("/sys/class/assist/accel/exist"));
        Log.d(TAG, "light-----------ic----------->"+readFileFile("/sys/class/assist/accel/ic"));
        Log.d(TAG, "light-----------factory----------->"+readFileFile("/sys/class/assist/accel/ic"));
        Log.d(TAG, "light-----------probe----------->"+readFileFile("/sys/class/assist/accel/ic"));
        Log.d(TAG, "camera1-----------factory----------->"+readFileFile("/sys/class/camera/rear_camera/module")+"-"+readFileFile("/sys/class/camera/rear_camera/ic")+"-"+readFileFile("/sys/class/camera/rear_camera/pixel")+"-"+readFileFile("/sys/class/camera/rear_camera/focus")+"-"+readFileFile("/sys/class/camera/rear_camera/fvalue")+"-"+readFileFile("/sys/class/camera/rear_camera/param")+"-"+readFileFile("/sys/class/camera/rear_camera/flash")+"-"+readFileFile("/sys/class/camera/rear_camera/pixel")+"-"+readFileFile("/sys/class/camera/rear_camera/probe"));
        Log.d(TAG, "camera2-----------factory----------->"+readFileFile("/sys/class/camera/front_camera/module")+"-"+readFileFile("/sys/class/camera/front_camera/ic")+"-"+readFileFile("/sys/class/camera/front_camera/pixel")+"-"+readFileFile("/sys/class/camera/front_camera/focus")+"-"+readFileFile("/sys/class/camera/front_camera/fvalue")+"-"+readFileFile("/sys/class/camera/front_camera/param")+"-"+readFileFile("/sys/class/camera/front_camera/flash")+"-"+readFileFile("/sys/class/camera/front_camera/pixel"));

        Log.d(TAG, "camera2-----------factory----------->"+readFileFile("/sys/class/camera/front_camera/module"));
        Log.d(TAG, "battery---------------------->"+readFileFile("/sys/class/power_supply/bms/battery_type"));
    }
    
    private String readFileFile(String filePath){   
        String res = "";  
        int ret = 0;
        try{   
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath))));
            String str=null;
            while((str=br.readLine())!=null){
                res+=str;
            }
        }   
        catch(Exception e){   
         e.printStackTrace();   
        }   
        return res;   
    }

	private String getBoardInfo() {
		String info = "";
		if (readFileFile("/sys/devices/soc0/hw_pcbboard_version") != null
				&& !readFileFile("/sys/devices/soc0/hw_pcbboard_version")
						.equals("")) {
			info = boardInfo + "-"
					+ readFileFile("/sys/devices/soc0/hw_pcbboard_version");
		} else {
			info = boardInfo;
		}

		return info;
	}

    @Override
    @Deprecated
    public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
        // TODO Auto-generated method stub
        super.setPreferenceScreen(preferenceScreen);
    
        setSummaryText(KEY_BOARD_ID,getBoardInfo());
        setSummaryText(KEY_TOUCHSCREEN_TYPE, getTouchScreenInfo());
        setSummaryText(KEY_CAMERA_TYPE, getCameraInfo(0));
        setSummaryText(KEY_CAMERA_TYPE1, getCameraInfo(1));
        setSummaryText(KEY_FLASH_TYPE,getFlashInfo() );
        setSummaryText(KEY_DDR_TYPE, getDdrInfo());
        setSummaryText(KEY_LCD_TYPE, getLCDInfo());
        setSummaryText(KEY_BATTERY_TYPE, readFileFile("/sys/class/power_supply/bms/battery_type"));
        setSummaryText(KEY_GESENSOR_TYPE, getGsensorInfo());
        setSummaryText(KEY_LIGHT_TYPE, getLightInfo());
        setSummaryText(KEY_WFBTFMGPS_TYPE, getBtInfo());
    }  
    
    
    private void setSummaryText(String preferenceKey, String value) {
        Preference preference = findPreference(preferenceKey);
        if (preference == null)
            return;

        if (TextUtils.isEmpty(value))
            preference.setSummary(mUnknown);
        else
            preference.setSummary(value);
    }
    
    private String getBtInfo() {
        String res = "";
        String btInfo = getString(R.string.factory)+SystemProperties.get("ro.wcnss.vendor",mUnknown)+"\n"+
        		getString(R.string.type)+SystemProperties.get("ro.wcnss.chipset",mUnknown)+"\n"+
        				getString(R.string.firmware)+SystemProperties.get("ro.wcnss.firmware.version",mUnknown);
        res = btInfo;
        return res;
    }

    private String getTouchScreenInfo() {
        String res = "";
        /*15-09-29 gaoshenpan fix touchFactoryInfo begin */
        String factoryInfo ="";
        String factory = (readFileFile("/sys/class/assist/ctp/tp_vendor") == null ? mUnknown
                        : readFileFile("/sys/class/assist/ctp/tp_vendor"));
        if(factory.equals(mUnknown)){
        	factoryInfo =getString(R.string.factory) + mUnknown;
        }else{
        	if(factory.equals("1")){
        		Log.d(TAG, "--------->"+factory+"k1");
        	}else if(factory.equals("2")){
        		Log.d(TAG, "--------->"+factory+"k2");
        	}
        	factoryInfo =getString(R.string.factory) + (factory.equals("1") ? getString(R.string.touchFactory_info2) : getString(R.string.touchFactory_info1));
        }
         
        String ic = getString(R.string.ic)
                + (readFileFile("/sys/class/assist/ctp/ic") == null ? mUnknown
                        : readFileFile("/sys/class/assist/ctp/ic"));
        String firmware = getString(R.string.firmware)
                +(readFileFile("/sys/class/assist/ctp/fw_ver") == null ? mUnknown
                        : readFileFile("/sys/class/assist/ctp/fw_ver"));
        res = factoryInfo + "\n" + ic + "\n" + firmware;
        /*15-09-29 gaoshenpan fix touchFactoryInfo end */
        return res;
    }

	private String getDdrInfo() {
		String res = "";
		String s = readFileFile("/sys/bus/mmc/devices/mmc0:0001/name");
		if (s != "" && s != null) {
			if (s.contains("HAG")) {
				res = getString(R.string.factory) + "HYNIX" + "\n" + "ROM&RAM:"
						+ "H9TQ17ABJTMCUR-KUM";
				Log.d(TAG, "res----->" + res);
				return res;
			} else if ("Q313MB".equals(s)) {
				res = getString(R.string.factory) + "SAMSUNG" + "\n"
						+ "ROM&RAM:" + "KMQ310013M-B419006";
				Log.d(TAG, "res----->" + res);
				return res;
			} else if ("R1J96N".equals(s)) {
				res = getString(R.string.factory) + "MICRON" + "\n"
						+ "ROM&RAM:" + "MT29TZZZ5D6YKFAH-125 W.96N";
				Log.d(TAG, "res----->" + res);
				return res;
			/*16-01-06 gaoshenpan fix show new ddrFlash dongzhi begin*/
			}else if ("016G32".equals(s)) {
				res = getString(R.string.factory) + "TOSHIBA" + "\n"
						+ "ROM&RAM:" + "TYE0HH231659RA";
				Log.d(TAG, "res----->" + res);
				return res;
				/*16-01-06 gaoshenpan fix show new ddrFlash dongzhi end*/
			}else {
				res = mUnknown;
				return res;
			}
		} else {
			res = mUnknown;
		}
		return res;
	}
    
	private String getFlashInfo() {
        Log.d(TAG, "----->" + getString(R.string.factory));
        String res = "";
        String factory = getString(R.string.factory)
                + (readFileFile("/sys/class/camera/rear_camera/flash_module") == null ? mUnknown
                        : readFileFile("/sys/class/camera/rear_camera/flash_module"));
        String ic = getString(R.string.ic)
                + (readFileFile("/sys/class/camera/rear_camera/flash") == null ? mUnknown
                        : readFileFile("/sys/class/camera/rear_camera/flash"));
        res = factory + "\n" + ic;

        return res;
    }
    private String getGsensorInfo() {
        Log.d(TAG, "----->" + getString(R.string.factory));
        String res = "";
        String factory = getString(R.string.factory)
                + (readFileFile("/sys/class/assist/accel/vendor") == null ? mUnknown
                        : readFileFile("/sys/class/assist/accel/vendor"));
        String ic = getString(R.string.ic)
                + (readFileFile("/sys/class/assist/accel/ic") == null ? mUnknown
                        : readFileFile("/sys/class/assist/accel/ic"));
        res = factory + "\n" + ic;

        return res;
    }
    
    private String getLightInfo() {
        String res = "";
        String factory = getString(R.string.factory)
                + (readFileFile("/sys/class/assist/alsps/vendor") == null ? mUnknown
                        : readFileFile("/sys/class/assist/alsps/vendor"));
        /*15-10-30 gaoshenpan fix get localString begin*/
        String ic = getString(R.string.ic)
                + (readFileFile("/sys/class/assist/alsps/ic") == null ? mUnknown
                : getLocatlName(readFileFile("/sys/class/assist/alsps/ic")));
        /*15-10-30 gaoshenpan fix get localString end*/
        res = factory + "\n" + ic;

        return res;
    }
   
    
	private String getLCDInfo() {
		String res = "";
		String[] strs,infos,items;
		String info = readFileFile("/sys/class/graphics/fb0/panel_name");
		Log.d(TAG, "getLCDInfo--info--->" + info);
		if (info != null) {
			 infos = info.split("=");
			Log.d(TAG, "getLCDInfo--infos--->" + infos[1]);
			 items = infos[1].split(" ");
			 if(items.length==9){
				 /*15-10-30 gaoshenpan fix get localString begin*/
					res = getString(R.string.ic_factory)+items[0]+"\n"
							+getString(R.string.glass_factory)+getString(R.string.glass_factory_name)+"\n"
							+getString(R.string.module_factory)+getLocatlName(items[4])+"\n"
							+getString(R.string.pixel_info)+items[1]+"\n"
							+getString(R.string.size_factory)+items[3];
					 /*15-10-30 gaoshenpan fix get localString end*/
				}
		}
		
		/*if (info != null) {
			strs = info.split("=");
			if (strs != null) {
				Log.d(TAG, "getLCDInfo----->" + strs[0]);
			}
		
		}*/

		return (res != null && res != "") ? res : mUnknown;
	}
	 /*15-10-30 gaoshenpan fix get localString begin*/
	private String getLocatlName(String s){
		String localName= "";
		if(s.contains("yashi")){
			localName = getString(R.string.module_factory_yashi);
		}else if(s.contains("dijing")){
			localName = getString(R.string.module_factory_dijing);
		}else if(s.contains("AP3426")){
			localName = s+"H";
		}
		
		return localName;
	}
	 /*15-10-30 gaoshenpan fix get localString end*/
    private String getCameraInfo( int i) {
        String res = "";
        String str1 = "";
        String str2 = "";
        String factory = readFileFile("/sys/class/camera/rear_camera/module");
        String type = readFileFile("/sys/class/camera/rear_camera/ic");
        String pix = readFileFile("/sys/class/camera/rear_camera/pixel");
        String xf = readFileFile("/sys/class/camera/rear_camera/focus");
        String fvalue = readFileFile("/sys/class/camera/rear_camera/fvalue");
        str1 = getString(R.string.rear_camera) + "\n"
                + getString(R.string.factory) + factory + "\n"
                + getString(R.string.ic) + type + "\n"
                + getString(R.string.pixel) + pix + "\n"
                + getString(R.string.focus) + xf + "\n"
                + getString(R.string.fvalue) + fvalue;
        Log.d(TAG, "str1----->" + str1);
        String factory1 = readFileFile("/sys/class/camera/front_camera/module");
        Log.d(TAG, "factory1----->" + factory1);
        String type1 = readFileFile("/sys/class/camera/front_camera/ic");
        String pix1 = readFileFile("/sys/class/camera/front_camera/pixel");
        String xf1 = readFileFile("/sys/class/camera/front_camera/focus");
        String fvalue1 = readFileFile("/sys/class/camera/front_camera/fvalue");
        str2 = getString(R.string.front_camera) + "\n"
                + getString(R.string.factory) + factory1 + "\n"
                + getString(R.string.ic) + type1 + "\n"
                + getString(R.string.pixel) + pix1 + "\n"
                + getString(R.string.focus1) + xf1 + "\n"
                + getString(R.string.fvalue) + fvalue1;
        Log.d(TAG, "str2----->" + str2);
        switch (i) {
		case 0:
			res = (str1 != null && str1 != "") ? str1 : mUnknown;
			break;
		case 1:
			res = (str2 != null && str2 != "") ? str2 : mUnknown;
			break;
		default:
			break;
		}
//        res = str1 + "\n" + str2;
        return (res != null && res != "") ? res : mUnknown;
    }

    

    
}
